package com.lagou.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.common.pojo.Goods;
import com.lagou.common.pojo.GoodsDTO;
import com.lagou.goods.mapper.GoodsMapper;
import com.lagou.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Goods queryById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public Integer deleteById(Integer id) {
        return goodsMapper.deleteById(id);
    }

    @Override
    public Integer updateById(Goods product) {
        return goodsMapper.updateById(product);
    }

    @Override
    public Map<String, Object> queryByPage(GoodsDTO goodDTO) {
        PageHelper.startPage(goodDTO.getStartPage(), goodDTO.getPageSize());
        List<Goods> goodsList =  goodsMapper.queryByPage(goodDTO);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);

        HashMap<String, Object> map = new HashMap<>(2);
        map.put("list", goodsList);
        map.put("total", pageInfo.getTotal());
        return map;
    }

    @Override
    public List<Goods> queryByIds(List<Integer> ids) {
        List<Goods> goodsList = goodsMapper.queryByIds(ids);
        return goodsList;
    }
}
